unit uTrackSegment;

interface

uses Graphics, uList, uTrackItem;

type

  TTrackSegmentStatus = (tsFree, tsOccupied, tsUnknown);

  TTrackSegment = class
  private
    aId: Cardinal;
    aAddress: Integer;
    aItems: TList;
    aStatus: TTrackSegmentStatus;
    aLocked: boolean;
    aOccupied: boolean;
    aCanvas: TCanvas;
  public
    constructor Create(paId: Cardinal; paCanvas: TCanvas);
    destructor  Destroy; override;

    procedure AddItem(paItem: TTrackItem);
    procedure Draw;
    procedure SetOccupied(paOccupied: boolean);
    function GetOccupied: boolean;

    property Id: Cardinal read aId;
    property Locked: boolean read aLocked write aLocked;
    property Occupied: boolean read aOccupied;
  end;

implementation

uses config;

{TTrackSegment}

procedure TTrackSegment.AddItem(paItem: TTrackItem);
begin
  aItems.Add(paItem);
  paItem.Segment := aId;
end;

constructor TTrackSegment.Create(paId: Cardinal; paCanvas: TCanvas);
begin
  inherited Create;
  aId := paId;
  aCanvas := paCanvas;
  aItems := TList.Create;
  aLocked := false;
  aOccupied := false;
end;

destructor TTrackSegment.Destroy;
begin
  aItems.Destroy;
  inherited Destroy;
end;

procedure TTrackSegment.Draw;
var
  i: TListIterator;
begin

  //draw item backgrounds
  aCanvas.Pen.Width := 1;
  i := TListIterator.Create(aItems);
  while i.HasNext do
  begin
    TTrackItem(i.Next).DrawBackGround;
  end;

  //draw items
  aCanvas.Pen.Width := 2;
  i.Reset;
  while i.HasNext do
  begin
    TTrackItem(i.Next).Draw;
  end;

  i.Destroy;
end;

procedure TTrackSegment.SetOccupied(paOccupied: boolean);
var
  i: TListIterator;
begin
  aOccupied := paOccupied;

  i := TListIterator.Create(aItems);
  while i.HasNext do
  begin
    TTrackItem(i.Next).Occupied := paOccupied;
  end;

  i.Destroy;
end;

function TTrackSegment.GetOccupied: boolean;
begin
  result := Occupied;
end;

end.
